package org.qpcr.community.project.web;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hswebframework.reactor.excel.ReactorExcel;
import org.hswebframework.web.api.crud.entity.QueryNoPagingOperation;
import org.hswebframework.web.authorization.annotation.Authorize;
import org.hswebframework.web.authorization.annotation.QueryAction;
import org.hswebframework.web.authorization.annotation.Resource;
import org.hswebframework.web.crud.service.ReactiveCrudService;
import org.hswebframework.web.crud.web.reactive.ReactiveServiceCrudController;
import org.qpcr.community.io.file.FileOption;
import org.qpcr.community.project.enums.ExportType;
import org.qpcr.community.project.model.entity.DataAnalysisExportAmplificationParam;
import org.qpcr.community.project.model.entity.ExperimentalRecordsInstanceEntity;
import org.qpcr.community.project.model.entity.ExportAmplificationParam;
import org.qpcr.community.project.model.entity.FileCenterInstanceEntity;
import org.qpcr.community.project.model.entity.excel.ExcelDataResponse;
import org.qpcr.community.project.service.DataAnalysisService;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.codec.multipart.FilePart;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

@RestController
@RequestMapping({"/project/data-analysis", "/project/data-analysis/records"})
@Authorize
@Resource(id = "data-analysis", name = "数据分析")
@Slf4j
@Tag(name = "数据分析接口")
@RequiredArgsConstructor
public class DataAnalysisController implements
    ReactiveServiceCrudController<ExperimentalRecordsInstanceEntity, String> {
    @Getter
    private final DataAnalysisService service;


    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @Authorize(ignore = true)
    @Operation(summary = "上传实验数据Excel文件")
    public Mono<ExcelDataResponse> uploadExcel(@RequestPart("file") FilePart filePart,String dataId) {
        return service.uploadExcel(filePart,dataId);
    }



    /**
     * 导出计算库的熔解扩增表格
     *
     * @param exportParam 导出
     */
    @PostMapping("/amplificationMetadada/export.{format}")
    @QueryAction
    @Authorize(ignore = true)
    @QueryNoPagingOperation(summary = "导出计算库的熔解扩增表格")
    public Mono<Void> exportAmplification(ServerHttpResponse response,@RequestBody DataAnalysisExportAmplificationParam exportParam) {
        return service.exportAmplification(response, exportParam);
    }
}
